/*
 *  loadlib32.cpp - part of LoadLib32
 *  Copyright (C) 2012  Dustin Escoffery
 *
 *  LoadLib32 is free software: you can redistribute it and/or
 *  modify it under the terms of the Lesser GNU General Public License
 *  as published by the Free Software Foundation, version 3 of the
 *  license.
 *
 *  LoadLib32 is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  Lesser GNU General Public License for more details.
 *
 *  You should have received a copy of the Lesser GNU General Public
 *  License along with LoadLib32.  If not, see:
 *      http://www.gnu.org/licenses/lgpl-3.0.txt
 *
 *  CONTRIBUTORS:
 *      Dustin Escoffery
 */

#include "inject.h"
#include <windows.h>

// Executes cmd in suspended mode
int CreateSuspendedProcess(const char* cmd, PPROCESS_INFORMATION lpi)
{
   STARTUPINFO si;
   ZeroMemory(&si, sizeof(si));
   si.cb = sizeof(si);
   ZeroMemory(lpi, sizeof(*lpi));
   return CreateProcess(NULL, (LPTSTR)cmd, NULL, NULL, 0,
      CREATE_SUSPENDED, NULL, NULL, &si, lpi);
}

int main(int argc, char* argv[])
{
   if (argc < 2)
   {
      FatalAppExit(0, "usage: loadlib32 exe_path [dll_path...]");
   }

   // Create the suspended process
   PROCESS_INFORMATION pi;
   CreateSuspendedProcess(argv[1], &pi);

   // Inject asm code to load the libs
   InjectLibs(pi, argc - 2, argv + 2);

   // Resume the suspended process
   ResumeThread(pi.hThread);

   return 0;
}
